package customer

import (
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
)

type CustomerHandler struct {
	*handler.Handler
	customerInfoService service.CustomerInfoService
}

func NewCustomerHandler(
	handler *handler.Handler,
	customerInfoService service.CustomerInfoService,
) *CustomerHandler {
	return &CustomerHandler{
		Handler:             handler,
		customerInfoService: customerInfoService,
	}
}

// Login 微信小程序登录接口
func (h *CustomerHandler) Login(ctx *gin.Context) {
	code := ctx.Param("code")
	if code == "" {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	token, err := h.customerInfoService.Login(ctx, code)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrUnauthorized, nil)
		return
	}
	v1.HandleSuccess(ctx, token)
}

// GetCustomerLoginInfo 获取用户的登录信息
func (h *CustomerHandler) GetCustomerLoginInfo(ctx *gin.Context) {
	customerID := ctx.GetInt64("ID")
	customerInfoVo, err := h.customerInfoService.GetCustomerInfo(ctx, customerID)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, customerInfoVo)
}

// UpdateWxPhoneNumber 更新用户微信手机号
func (h *CustomerHandler) UpdateWxPhoneNumber(ctx *gin.Context) {
	var updateWxPhoneForm v1.UpdateWxPhoneForm
	if err := ctx.ShouldBindJSON(&updateWxPhoneForm); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	ok, err := h.customerInfoService.UpdateWxPhoneNumber(ctx, &updateWxPhoneForm)
	if err != nil {
		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
		return
	}
	v1.HandleSuccess(ctx, ok)
}




